package GUI;


import java.awt.*;
import javax.swing.*; 
import java.awt.event.*;
import java.util.EventListener;
public class Test extends JFrame  implements ActionListener {
    private JPanel jpnlTop, jpnlCenter, jpnlBottom, jpnlHead, jpnlFoot, jpnlLeft, jpnlMiddle, jpnlRight, jpnlQuestion, jpnlAnswer;
    private JButton jbtnExecute, jbtnFinish, jbtnReset;
    private JLabel jlblTitle;
    private Label jlblUser, jlblPassword, jlblSubject, jlblTrue, jlblFalse, jlblResult, jlblTime, jlblQuestions, jlblAnswers, jlblPosition, jlblA, jlblB, jlblC, jlblD;
    private Label[] jlblNumber;
    private JTextField jtxtUser, jtxtPassword, jtxtTrue, jtxtFalse, jtxtResult, jtxtTime, jtxtQuestion, jtxtAnswer;
    private Choice chSubject;
    private GridBagLayout gdLayout;
    private GridBagConstraints gdb;
    private TextArea jtxaQuestion;
    private JScrollPane jsplLeft, jsplRight;
    private JCheckBox[][] jchk;
    private String[] strSwitch = {"", "a", "b", "c", "d"};
    private int[] iQuestion; 
    public Test()
    {
    	super("Test");
    	this.setLayout(new BorderLayout());
    	this.jpnlTop = new JPanel();
    	this.jpnlCenter = new JPanel();
    	this.jpnlBottom = new JPanel();
    	this.add(jpnlTop, BorderLayout.NORTH);
    	this.add(jpnlCenter, BorderLayout.CENTER);
    	this.add(jpnlBottom, BorderLayout.SOUTH);
    	///set layout cho tung thanh phan
    	//panel top
    	this.jlblTitle = new JLabel("CHUONG TRINH KIEM TRA TRAC NGHIEM TREN MAY TINH");
    	this.jlblTitle.setForeground(Color.blue);
    	this.jlblTitle.setSize(30, 30);
    	this.jpnlTop.add(jlblTitle);
    	//set thanh phan bottom
    	this.jbtnExecute = new JButton("Lam bai");
    	this.jbtnFinish = new JButton("Hoan thanh");
    	this.jbtnReset = new JButton("Lam lai");
    	this.jbtnExecute.setForeground(Color.red);
    	this.jbtnFinish.setForeground(Color.red);
    	this.jbtnReset.setForeground(Color.red);
    	this.jpnlBottom.add(jbtnExecute);
    	this.jpnlBottom.add(jbtnFinish);
    	this.jpnlBottom.add(jbtnReset);
    	
    	//////////center panel
    	this.gdLayout = new GridBagLayout();
    	this.gdb = new GridBagConstraints();
    	this.jpnlCenter.setLayout(new BorderLayout(10, 10));
    	this.jpnlHead = new JPanel();
    	this.jpnlFoot = new JPanel();
    	this.jpnlCenter.add(jpnlHead, BorderLayout.NORTH);
    	this.jpnlCenter.add(jpnlFoot, BorderLayout.CENTER);
    	////////head panel
    	this.jpnlLeft = new JPanel();
    //	this.jpnlMiddle = new JPanel();
    	this.jpnlRight = new JPanel();
    	this.jpnlHead.setLayout(gdLayout);
    	////////left panel
    	this.buildConstraints(gdb, 0, 0, 1, 1, 40, 100);
    	this.gdb.fill = GridBagConstraints.BOTH;
    	this.gdLayout.setConstraints(jpnlLeft, gdb);
    	this.jpnlHead.add(jpnlLeft);
    	
    	////////right pane
    	this.buildConstraints(gdb, 1, 0, 1, 1, 60, 100);
    	this.gdb.fill = GridBagConstraints.BOTH;
    	this.gdLayout.setConstraints(jpnlRight, gdb);
    	this.jpnlHead.add(jpnlRight);
    	///////add componnent vao left panel
    	this.jpnlLeft.setLayout(gdLayout);
    	
    	this.buildConstraints(gdb, 0, 0, 1, 1, 10, 30);
	gdb.fill = GridBagConstraints.NONE;
	gdb.anchor = GridBagConstraints.WEST;
	this.jlblUser = new Label("Sinh vien: ", Label.RIGHT);
	gdLayout.setConstraints(jlblUser, gdb);
	this.jpnlLeft.add(jlblUser);
	///////
	this.buildConstraints(gdb, 1, 0, 1, 1, 90, 0);
	gdb.fill = GridBagConstraints.HORIZONTAL;
	//gdb.anchor = GridBagConstraints.;
	this.jtxtUser = new JTextField();
	gdLayout.setConstraints(jtxtUser, gdb);
	this.jpnlLeft.add(jtxtUser);
	//////
	this.buildConstraints(gdb, 0, 1, 1, 1, 0, 30);
	gdb.fill = GridBagConstraints.NONE;
	gdb.anchor = GridBagConstraints.WEST;
	this.jlblPassword = new Label("MSSV: ", Label.RIGHT);
	gdLayout.setConstraints(jlblPassword, gdb);
	this.jpnlLeft.add(jlblPassword);
	///////
	this.buildConstraints(gdb, 1, 1, 1, 1, 0, 0);
	gdb.fill = GridBagConstraints.HORIZONTAL;
	//gdb.anchor = GridBagConstraints.;
	this.jtxtPassword = new JTextField();
	gdLayout.setConstraints(jtxtPassword, gdb);
	this.jpnlLeft.add(jtxtPassword);
	///////
	this.buildConstraints(gdb, 0, 2, 1, 1, 0, 30);
	gdb.fill = GridBagConstraints.NONE;
	gdb.anchor = GridBagConstraints.WEST;
	this.jlblSubject = new Label("Mon: ", Label.RIGHT);
	gdLayout.setConstraints(jlblSubject, gdb);
	this.jpnlLeft.add(jlblSubject);
	/////
	this.buildConstraints(gdb, 1, 2, 1, 1, 0, 0);
	gdb.fill = GridBagConstraints.HORIZONTAL;
	//gdb.anchor = GridBagConstraints.;
	this.chSubject = new Choice();
	gdLayout.setConstraints(chSubject, gdb);
	this.jpnlLeft.add(chSubject);
	//////////right panel
	this.jpnlRight.setLayout(new GridLayout(3, 4, 5, 5));
	this.jlblTrue = new Label("Dung: ", Label.RIGHT);
	this.jlblFalse = new Label("Sai: ", Label.RIGHT);
	this.jlblResult = new Label("Diem: ", Label.RIGHT);
	this.jlblTime = new Label("Thoi gian: ", Label.RIGHT);
	this.jlblQuestions = new Label("So cau hoi: ", Label.RIGHT);
	this.jlblAnswers = new Label("Cau tra loi: ", Label.RIGHT);
	this.jtxtTrue = new JTextField();
	this.jtxtFalse = new JTextField();
	this.jtxtResult = new JTextField();
	this.jtxtTime = new JTextField();
	this.jtxtQuestion = new JTextField();
	this.jtxtAnswer = new JTextField();
	this.jpnlRight.add(jlblTrue);
	this.jpnlRight.add(jtxtTrue);
	this.jpnlRight.add(jlblTime);
	this.jpnlRight.add(jtxtTime);
	this.jpnlRight.add(jlblFalse);
	this.jpnlRight.add(jtxtFalse);
	this.jpnlRight.add(jlblQuestions);
	this.jpnlRight.add(jtxtQuestion);
	this.jpnlRight.add(jlblResult);
	this.jpnlRight.add(jtxtResult);
	this.jpnlRight.add(jlblAnswers);
	this.jpnlRight.add(jtxtAnswer);
	//////question panel
	
	this.jtxaQuestion = new TextArea();
	this.jpnlQuestion = new JPanel();
	this.jpnlQuestion.add(jtxaQuestion);
	//////answer panel
	this.jpnlAnswer = new JPanel();
	/////////foot panel
	this.jpnlFoot.setLayout(gdLayout);
	this.buildConstraints(gdb, 0, 0, 1, 1, 75, 100);
	this.gdb.fill = GridBagConstraints.BOTH;
	this.jsplLeft = new JScrollPane(jtxaQuestion, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
	this.gdLayout.setConstraints(jsplLeft, gdb);
	this.jpnlFoot.add(jsplLeft);
	///////////
	this.jpnlFoot.setLayout(gdLayout);
	this.buildConstraints(gdb, 1, 0, 1, 1, 25, 100);
	this.gdb.fill = GridBagConstraints.BOTH;
	this.jsplRight = new JScrollPane(this.createCheckbox(this.jpnlAnswer), JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane. HORIZONTAL_SCROLLBAR_NEVER); //check box
	this.gdLayout.setConstraints(jsplRight, gdb);
	this.jpnlFoot.add(jsplRight);
	/////disable cac textfield
	this.jtxtAnswer.setEditable(false);
	this.jtxtFalse.setEditable(false);
	this.jtxtResult.setEditable(false);
	this.jtxtTime.setEditable(false);
	this.jtxtQuestion.setEditable(false);
	this.jtxtTrue.setEditable(false);
	this.chSubject.setEnabled(true);
    	///////set kich thuoc va hien
    	this.setSize(900, 700);
        this.setResizable(false);
    	this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    	this.setLocation(150, 0);
    	//////cac su kien voi nut button
    	this.jbtnExecute.addActionListener(new eventExecute());
    	this.jbtnFinish.addActionListener(new eventFinish());
    	this.jbtnReset.addActionListener(new eventReset());
    }
    //Phuong thuc tao constraints cho cac doi tuong component
    private void buildConstraints(GridBagConstraints gdb, int gx, int gy, int gw, int gh, int wx, int wy)
    {
    	gdb.gridx = gx;
    	gdb.gridy = gy;
	gdb.gridwidth = gw;
	gdb.gridheight = gh;
	gdb.weightx = wx;
	gdb.weighty = wy;
    }
    ///phuong thuc tao cac checkbox
    private JPanel createCheckbox(JPanel jpnl)
    {
    	int iSum = 0;
	jpnl.setLayout(new GridLayout(51, 5, 10, 10));
	this.jchk = new JCheckBox[50][4];
	this.iQuestion = new int[50];
	for(int iCount = 0; iCount < 50; iCount++)
	{
		iSum++;
		iQuestion[iCount] = iSum;
	}
	for(int iCount = 0; iCount < 51; iCount++)
	{
		for(int iIndex = 0; iIndex < 5; iIndex++)
		{
			if (iCount == 0)
			{
				jpnl.add(new Label(strSwitch[iIndex]));
			}
			else if (iIndex == 0 && iCount != 0)
			{
				jpnl.add(new Label(String.valueOf(iQuestion[iCount - 1])));
			}
			else
			{
				this.jchk[iCount - 1][iIndex - 1] = new JCheckBox();
				jpnl.add(jchk[iCount - 1][iIndex - 1]);
			}
		}
	}
	return jpnl;
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    //////////
    class eventExecute  implements ActionListener {
        @Override
	public void actionPerformed(ActionEvent e) {
            jtxtUser.setEditable(false);
            jtxtPassword.setEditable(false);
	}
    }

	class eventFinish  implements ActionListener {
            @Override
		public void actionPerformed(ActionEvent e) {
			// TODO: Add your code here
			
		}
	}
	
	class eventReset  implements ActionListener {
            @Override
		public void actionPerformed(ActionEvent e) {
			// TODO: Add your code here;
			jtxtUser.setEditable(true);
			jtxtPassword.setEditable(true);
			jtxtUser.setText("");
			jtxtUser.requestFocus();
		}
	}
}
